package com.ymatou.i.accounting.api.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.i.accounting.api.parameter.GetUserAccountBalanceListBean;
import com.ymatou.i.accounting.api.parameter.UserAccount;
import com.ymatou.i.accounting.api.parameter.UserAccountBalance;
import com.ymatou.i.accounting.api.service.GetUserAccountBalanceListCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AccountCallService;
import com.ymttest.business.service.JUserCallService;
/**
*批量查询用户余额
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserAccountBalanceList{
	private static GetUserAccountBalanceListBean getuseraccountbalancelistBean;
	private static GetUserAccountBalanceListCall getuseraccountbalancelistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询用户余额");
	}

	@Before
	public void caseUp() {
		getuseraccountbalancelistBean=new GetUserAccountBalanceListBean();
		getuseraccountbalancelistCall=new GetUserAccountBalanceListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuseraccountbalancelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * @param count 生成个数
	 * @param type 账户类型
	 * @return
	 * @throws Exception
	 */
	private Map<String,Map> initUserAccountBalance(int count,int type) throws Exception{
		Map<String,Map> userbalances=new HashMap<String,Map>();
		for(int i=0;i<count;i++){
			//初始化账号和余额
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			Integer uid=JUserCallService.UserRegisterInfoRequestDtoCall(urb).getUserid();
			String aid=AccountCallService.getAccountId(uid,type);
			Integer newAmount=new Random().nextInt(10)+1;
			Integer newFreeze=new Random().nextInt(10)+1;
			 
			AccountCallService.resetAccount(aid, new BigDecimal(newAmount), new BigDecimal(newFreeze));
			//插入返回数据
			Map m=new HashMap<>();
			m.put("uid", uid);
			m.put("AvailAmount", newAmount);
			m.put("FreezeAmount", newFreeze);
			m.put("aid", aid);
			userbalances.put(aid, m);
		}
		return userbalances;
		
	}
	private void verifyDB(Map<String, Map> mUserAccountBalance, ArrayList<UserAccountBalance> list, Integer type) {
		for (UserAccountBalance userAccountBalance : list) {
			Map msource = mUserAccountBalance.get(userAccountBalance.getAccountId());
			if(msource!=null){
				Logger.verifyEquals(msource.get("uid").toString(), userAccountBalance.getUserId().toString(), "验证uid");
				Logger.verifyEquals(type.toString(), userAccountBalance.getAccountType().toString(), "验证type");
				Logger.verifyEquals(new BigDecimal(msource.get("AvailAmount").toString()).setScale(2), new BigDecimal(userAccountBalance.getAvailAmount()).setScale(2), "验证AvailAmount");
				Logger.verifyEquals(new BigDecimal(msource.get("FreezeAmount").toString()).setScale(2), new BigDecimal(userAccountBalance.getFreezeAmount()).setScale(2), "验证FreezeAmount");
			}else{
				Logger.verifyEquals("0", "1", "源数据为null,失败");
			}
		}
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserAccountBalanceList_001() {
		Logger.start(true,"批量查询单个用户余额成功");
		try {
			int type=1;
			Map<String, Map> mUserAccountBalance=initUserAccountBalance(1,type);
			
			getuseraccountbalancelistBean.setAppId("autotest");
			List<UserAccount> UserAccountList=new ArrayList<>();
			 Collection<Map> accountblist = mUserAccountBalance.values();
			for (Map m : accountblist) {
				UserAccount uaccount=new UserAccount();
				uaccount.setUserId((int)m.get("uid"));
				uaccount.setAccountType(type);
				UserAccountList.add(uaccount);
			}
			getuseraccountbalancelistBean.setUserAccountList(UserAccountList);
			getuseraccountbalancelistCall.setData(getuseraccountbalancelistBean);
			getuseraccountbalancelistCall.callService();			
			Logger.verifyEquals("0", getuseraccountbalancelistCall.getString("StatusCode"), "验证StatusCode");
			Logger.verifyEquals("操作成功！", getuseraccountbalancelistCall.getString("Message"), "验证Message");
			ArrayList<UserAccountBalance> list = getuseraccountbalancelistCall.getUserAccountBalanceList();
			verifyDB(mUserAccountBalance, list,type);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetUserAccountBalanceList_002() {
		Logger.start(true,"不输入UserAccountList失败");
		try {
			getuseraccountbalancelistBean.setAppId("autotest");
			getuseraccountbalancelistCall.setData(getuseraccountbalancelistBean);
			getuseraccountbalancelistCall.callService();			
			Logger.verifyEquals("2", getuseraccountbalancelistCall.getString("StatusCode"), "验证StatusCode");
			Logger.verifyEquals("UserAccountList不能为空！", getuseraccountbalancelistCall.getString("Message"), "验证Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetUserAccountBalanceList_003() {
		Logger.start(true,"不输入UserId,不返回数据");
		try {
			int type=1;
			Map<String, Map> mUserAccountBalance=initUserAccountBalance(1,type);
			
			getuseraccountbalancelistBean.setAppId("autotest");
			List<UserAccount> UserAccountList=new ArrayList<>();
			 Collection<Map> accountblist = mUserAccountBalance.values();
			for (Map m : accountblist) {
				UserAccount uaccount=new UserAccount();
//				uaccount.setUserId((int)m.get("uid"));
				uaccount.setAccountType(type);
				UserAccountList.add(uaccount);
			}
			getuseraccountbalancelistBean.setUserAccountList(UserAccountList);
			getuseraccountbalancelistCall.setData(getuseraccountbalancelistBean);
			getuseraccountbalancelistCall.callService();		
			Logger.verifyEquals("0", getuseraccountbalancelistCall.getString("StatusCode"), "验证StatusCode");
			Logger.verifyEquals("操作成功！", getuseraccountbalancelistCall.getString("Message"), "验证Message");
			Logger.verifyEquals(0, getuseraccountbalancelistCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("UserAccountBalanceList").size(), "验证UserAccountBalanceList为size0");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetUserAccountBalanceList_004() {
		Logger.start(true,"不输入AccountType,不返回数据");
		try {
			int type=1;
			Map<String, Map> mUserAccountBalance=initUserAccountBalance(1,type);
			
			getuseraccountbalancelistBean.setAppId("autotest");
			List<UserAccount> UserAccountList=new ArrayList<>();
			 Collection<Map> accountblist = mUserAccountBalance.values();
			for (Map m : accountblist) {
				UserAccount uaccount=new UserAccount();
				uaccount.setUserId((int)m.get("uid"));
//				uaccount.setAccountType(type);
				UserAccountList.add(uaccount);
			}
			getuseraccountbalancelistBean.setUserAccountList(UserAccountList);
			getuseraccountbalancelistCall.setData(getuseraccountbalancelistBean);
			getuseraccountbalancelistCall.callService();		
			Logger.verifyEquals("0", getuseraccountbalancelistCall.getString("StatusCode"), "验证StatusCode");
			Logger.verifyEquals("操作成功！", getuseraccountbalancelistCall.getString("Message"), "验证Message");
			Logger.verifyEquals(0, getuseraccountbalancelistCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("UserAccountBalanceList").size(), "验证UserAccountBalanceList为size0");
				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserAccountBalanceList_005() {
		Logger.start(true,"批量查询多个用户余额成功");
		try {
			int type=1;
			Map<String, Map> mUserAccountBalance=initUserAccountBalance(5,type);
			
			getuseraccountbalancelistBean.setAppId("autotest");
			List<UserAccount> UserAccountList=new ArrayList<>();
			 Collection<Map> accountblist = mUserAccountBalance.values();
			for (Map m : accountblist) {
				UserAccount uaccount=new UserAccount();
				uaccount.setUserId((int)m.get("uid"));
				uaccount.setAccountType(type);
				UserAccountList.add(uaccount);
			}
			getuseraccountbalancelistBean.setUserAccountList(UserAccountList);
			getuseraccountbalancelistCall.setData(getuseraccountbalancelistBean);
			getuseraccountbalancelistCall.callService();			
			Logger.verifyEquals("0", getuseraccountbalancelistCall.getString("StatusCode"), "验证StatusCode");
			Logger.verifyEquals("操作成功！", getuseraccountbalancelistCall.getString("Message"), "验证Message");
			ArrayList<UserAccountBalance> list = getuseraccountbalancelistCall.getUserAccountBalanceList();
			verifyDB(mUserAccountBalance, list,type);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserAccountBalanceList_006() {
		Logger.start(true,"批量查询非普通用户余额成功(AccountType!=1)");
		try {
			int type=15;
			Map<String, Map> mUserAccountBalance=initUserAccountBalance(1,type);
			
			getuseraccountbalancelistBean.setAppId("autotest");
			List<UserAccount> UserAccountList=new ArrayList<>();
			 Collection<Map> accountblist = mUserAccountBalance.values();
			for (Map m : accountblist) {
				UserAccount uaccount=new UserAccount();
				uaccount.setUserId((int)m.get("uid"));
				uaccount.setAccountType(type);
				UserAccountList.add(uaccount);
			}
			getuseraccountbalancelistBean.setUserAccountList(UserAccountList);
			getuseraccountbalancelistCall.setData(getuseraccountbalancelistBean);
			getuseraccountbalancelistCall.callService();			
			Logger.verifyEquals("0", getuseraccountbalancelistCall.getString("StatusCode"), "验证StatusCode");
			Logger.verifyEquals("操作成功！", getuseraccountbalancelistCall.getString("Message"), "验证Message");
			ArrayList<UserAccountBalance> list = getuseraccountbalancelistCall.getUserAccountBalanceList();
			verifyDB(mUserAccountBalance, list,type);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}